package ar.edu.caece.pl.asem.model.impl.treeelements;

import ar.edu.caece.pl.asem.model.impl.SymbolTable;
import ar.edu.caece.pl.asem.temp.TemporalGenerator;

public class Variable extends SimboloGenerico {
	
	public Variable(String name, int type) {
		super(name, type);
		this.label = "VARIABLE";
	}
	
	@Override
	public String toString() {
		return this.getLabel() + ": " + this.getName() + " (" + this.getEnvName() + " " + SymbolTable.getInstance().getVerboseType(this.getType()) + ")";
	}

	@Override
	public void generateASMCode() {
		StringBuffer sb = new StringBuffer();
		
		if(!(this.parent instanceof Declaraciones)){
			if(this.getEnvName().equalsIgnoreCase("MAIN")||this.getEnvName().equalsIgnoreCase("GLOBAL")){
			TemporalGenerator.getInstance().generateTemporal();			
			this.place = TemporalGenerator.getInstance().getActualTemporal();
			sb.append(TAB + "mov ax, " + this.getEnvName() + "_" + this.getName() + ENTER);
			sb.append(TAB + "mov " + this.place + ", ax" + ENTER);
		}else{
			TemporalGenerator.getInstance().generateTemporal();
			this.place = TemporalGenerator.getInstance().getActualTemporal();
			sb.append(TAB + "mov ax, " + "[bp-"+ this.getMemoryInStack() +']' + ENTER);
			sb.append(TAB + "mov " + this.place + ",ax" + ENTER);	
		}
			
			
		} else {
			if(this.parent.parent.getName().equalsIgnoreCase("MAIN")){
				sb.append(TAB + this.getEnvName() + "_" + this.getName() + " dw 0" + ENTER);
			}
		}
		this.code = sb.toString();
		
	}

}
